// pages/operators/index/index.js

var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vInfo: null,
    info: null,
    userName: "",
    password: ""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkLogin();
  },
  changeInput(e) {
    let changed = {};
    let prop = e.currentTarget.dataset.prop
    changed[prop] = e.detail.value
    this.setData(changed)
  },
  checkLogin() {
    let self = this;
    let vInfo = wx.getStorage({
      key: 'vInfo',
      success: function (res) {
        let vInfo = res.data;
        if (vInfo) {
          self.setData({
            vInfo
          });
          self.getAmount();
          self.getOrders();
        }
      },
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getOrders: function () {
    let vendorId = this.data.vInfo.id;
    let self = this;
    util.request(api.orderIndex, {
      vendorId
    }).then(r => {
      self.setData({
        c: r.data
      })
    })
  },
  login: function () {
    let {
      userName,
      password
    } = this.data;
    let self = this;
    if (!userName) {
      wx.showToast({
        title: '请输入用户名',
        icon: 'none'
      })
      return;
    }
    if (!password) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
      return;
    }
    let userInfo = wx.getStorageSync('userInfo');
    wx.login({
      success: function (res) {
        if (res.code) {
          util.request(api.VLBW, {
            code: res.code,
            userInfo
          }, 'JSON').then(r => {
            console.log(13, r);
            let {
              openId
            } = r.data;
            util.request(api.VLogin, {
              userName,
              password,
              openId
            }, "JSON").then(r => {
              if (!r.errno) {
                wx.hideToast();
                wx.setStorage({
                  key: 'vInfo',
                  data: r.data
                });
                self.setData({
                  vInfo: r.data
                });
              } else {
                wx.hideToast();
                wx.showToast({
                  title: r.errmsg,
                  icon: 'none'
                })
              }
            }).catch(err => {
              wx.hideToast();
              wx.showToast({
                title: err,
                icon: 'none'
              })
            })
          })
        } else {
          wx.hideToast();
          wx.showToast({
            title: '登录失败',
            icon: 'none'
          })
        }
      },
      fail: function (err) {
        wx.hideToast();
        wx.showToast({
          title: err,
          icon: 'none'
        })
      }
    });
  },
  getAmount() {
    let vendorId = this.data.vInfo.id;
    let self = this;
    util.request(api.VAmount, {
      vendorId
    }).then(r => {
      self.setData({
        info: r.data
      })
    })
  }
})